package com.itheima.homework;

import java.util.Collections;
import java.util.TreeSet;

/**
 * @Author 王加鑫
 * @Date 2021/5/14 19:25
 * @Version 1.0
 * 3、【编号：1506】在某次考试中，学生的成绩信息如下： 姓名 年龄 成绩 Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100 Lucy 22 90 Kevin 22 90
 * 请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序，成绩和年龄都一样，则按照姓名的字典顺序排序。
 */
public class Demo3 {
    public static void main(String[] args) {
        //method1();
        method2();


    }

    private static void method2() {
        Student student1 = new Student("Tome",20,90);
        Student student2 = new Student("Jerry",22,95);
        Student student3 = new Student("John",20,100);
        Student student4 = new Student("Lily",22,100);
        Student student5 = new Student("Lucy",22,90);
        Student student6 = new Student("Kevin",22,90);
        TreeSet<Student> set = new TreeSet<>(
                (o1, o2) -> {
                    int res = o1.getScore() - o2.getScore();
                    res = res == 0 ? o1.getName().compareTo(o2.getName()) : res == 0 ? o1.getAge() - o2.getAge() : res;
                    return res;
                }
        );
        Collections.addAll(set,student1,student2,student3,student4,student5,student6);
        for (Student student : set) {
            System.out.println(student);
        }
    }

    private static void method1() {
        Student student1 = new Student("Tome",20,90);
        Student student2 = new Student("Jerry",22,95);
        Student student3 = new Student("John",20,100);
        Student student4 = new Student("Lily",22,100);
        Student student5 = new Student("Lucy",22,90);
        Student student6 = new Student("Kevin",22,90);

        TreeSet<Student> set = new TreeSet<>();
        Collections.addAll(set,student1,student2,student3,student4,student5,student6);
        for (Student student : set) {
            System.out.println(student);
        }
    }
}


class Student implements Comparable<Student>{
    private String name;
    private int age;
    private int score;

    public Student() {
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int res = o.score - this.score;
        res = res == 0 ? o.name.compareTo(this.name) : res == 0 ? o.age - this.age : res;
        return res;
    }
}
